package com.wwh.furn.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wwh.furn.bean.Furn;
import com.wwh.furn.bean.Msg;
import com.wwh.furn.service.FurnService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Albert
 * @date 2025/2/2-11:52
 * @describe
 */
@Controller
//响应客户端的请求
public class FurnController {

    @Resource
    private FurnService furnService;


    /**
     * 1.响应客户端的添加请求
     * 2.springMVC的@RequestBody将客户端提交的json数据封装成JavaBean对象
     * 3.@ResponseBody 服务器按照Json格式返回数据（底层按照http协议进行协商）
     * @param furn
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Msg save(@Validated @RequestBody Furn furn, Errors errors) {
        Map<String, Object> filedErrors = new HashMap<>();
        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            filedErrors.put(fieldError.getField(), fieldError.getDefaultMessage());
        }

        if (filedErrors.isEmpty()){
            furnService.save(furn);
            //返回成功信息
            return Msg.success();
        } else {
            //返回失败信息
            return Msg.fail().add("filedErrors", filedErrors);
        }


    }

    @RequestMapping("/furns")
    @ResponseBody
    public Msg listFurns() {
        List<Furn> furnList = furnService.findAll();
        //把家居信息封装到Msg，然后返回给前端
        return Msg.success().add("furnList", furnList);
    }

    @PutMapping("/update")
    @ResponseBody
    public Msg updateFurn(@RequestBody Furn furn) {
        furnService.update(furn);
        //把家居信息封装到Msg，然后返回给前端
        return Msg.success();
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Msg delFurn(@PathVariable Integer id) {
        furnService.delete(id);
        return Msg.success();
    }

    @RequestMapping("/furn")
    @ResponseBody
    public Msg furn(@RequestParam Integer id) {
        Furn furn = furnService.findFurnById(id);
        return Msg.success().add("furn", furn);
    }

    /**
     * 分页查询家居信息
     * @param pageNum:要显示第几页
     * @param pageSize:每页要显示几条数据
     *
     */
    @ResponseBody
    @RequestMapping("/furnsByPage")
    public Msg listFurnByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize) {
        //设置分页参数
        //1.调用findALL是完成查询，底层会进行物理分页，而不是逻辑分页
        //2.会根据分页参数来计算limit？，？，在发出SQL语句时，会带Limit
        //3.我们后面会给大家抓取SQL
        PageHelper.startPage(pageNum, pageSize);
        List<Furn> furns = furnService.findAll();

        PageInfo<Furn> furnPageInfo = new PageInfo<>(furns, pageSize);
        return Msg.success().add("furnPageInfo", furnPageInfo);
    }

    /**
     * 根据家具名分页查询家居信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/furnsByConditionPage")
    public Msg listFurnByConditionPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "5") Integer pageSize,
                                       @RequestParam(defaultValue = "") String search) {
        //设置分页参数
        //1.调用findALL是完成查询，底层会进行物理分页，而不是逻辑分页
        //2.会根据分页参数来计算limit？，？，在发出SQL语句时，会带Limit
        //3.我们后面会给大家抓取SQL
        PageHelper.startPage(pageNum, pageSize);
        List<Furn> furns = furnService.findByCondition(search);

        PageInfo<Furn> furnPageInfo = new PageInfo<>(furns, pageSize);
        return Msg.success().add("furnPageInfo", furnPageInfo);
    }
}
